#pragma once
//============================================================================
// Copyright (C) 2013 Brett R. Jones
//
// Issued to public domain 2013
//
// brettjones1900@gmail.com
// http://www.p2panarchy.com
//============================================================================

#include "VxDefs.h"

//=== VxTimer Class object ===//
//there are a couple of ways to use the timer
//		any call to StartTime with any value sets the start time
//		so that calls to ElapsedTime return the Time since last called StartTimer or timer was constucted,

//		The second mode is StartTimer with a time till done
//		Subsequent calls to TimeDone return FALSE if the specified TimeTillDone has not been
//		reached or TRUE if the time has expired
//
//		NOTE: even after the TimerDone has expired, calls to ElapsedTimer are still valid
//! query performance counter
void VxQueryCounter( unsigned long & u32RetRandNum );
//! query performance counter
void VxQueryCounter( U64 & i64RetRandNun );

class VxTimer
{
private:
	U64		m_i64StartTick;
	double	m_dTimeTillDone;

	static bool		m_bCounterInitialized;
	static bool		m_bHaveMultipleCPUS;
	static U64		m_i64TimerFrequency;
	static double	m_dInverseFrequency;

public:
	VxTimer();
	virtual ~VxTimer();
	double		ElapsedSec( void ); // return the elapsed time in seconds since
									// StartTimer was called or VxTimer was constructed...it doesnt mater if the time
									// specified in the StartTimer parameter has elapsed or not
	double		ElapsedMilliSec( void ){return (ElapsedSec()*1000);}
	double		ElapsedMicroSec( void ){return (ElapsedSec()*1000000);};
	//just sets start time for elapsed time
	void		StartTimer( void ){ StartTimer(0.0); }
	//starts a timer and sets time till TimerDone returns true
	void		StartTimer( double dSec  );
	void		StartMilliTimer( double dMilliSec ){ StartTimer( dMilliSec * 1000.0 );}
	void		StartMicroTimer( double dMicoSec ){ StartTimer( dMicoSec * 1000000.0 );}
	BOOL		TimerDone( void ){return ElapsedSec() * 1000000 > m_dTimeTillDone?1:0;}

	void		WaitTime( double dMicroSec );

	static void		queryPerformanceCounter( U64& s64Tick );
	static void		init( void );//this function only added for instances when constructor doesnt get called

};


